// "use strict"
let display = document.getElementById('display')
let btn = document.getElementById('btn')
let input = document.getElementById('input')
let time = document.getElementById('time')
let arr = [2]
let currentNumber = 2
let maxNumber = 0
btn.addEventListener('click', function() {
  if (!input.value) {
    display.innerHTML = '请输入范围'
    return
  }
  if (!+input.value) {
    display.innerHTML = '请输入正确数字'
    return
  }
  if (input.value < 2) {
    display.innerHTML = '范围太小'
    return
  }
  arr = [2]
  maxNumber = +input.value
  getPrimeNumer()
})

async function getPrimeNumer() {
  time.innerHTML = ''
  display.innerHTML = '正在计算...'
  let oldTime = new Date().getTime()
  await addPrimeNumber(2)
  let html = arr.join(', ')
  let timeHtml = `${new Date().getTime() - oldTime}ms`
  setTimeout(() => {
    display.innerHTML = html
    time.innerHTML = timeHtml
  }, 1000)
}

function addPrimeNumber() {
  for (currentNumber = arr[0]; currentNumber <= maxNumber; currentNumber++) {
    if (arr.every(item => currentNumber % item !== 0)) {
      arr.push(currentNumber)
    }
  }
}